﻿function refreshPlugin(id) {
    var w = window;
    while (w != w.parent && w.parent != null) {
        w = w.parent;
    }

    w.doRefreshPlugin(w, id);
}

function setContext(text) {
    var w = window;
    while (w != w.parent && w.parent != null) {
        w = w.parent;
    }

    w.doSetContext(text);
}



function doRefreshPlugin(w, id) {
    //
    //  ok, here we are about to do the refresh
    scanFramesForPlugin(w, id);
}

function scanFramesForPlugin(w, id) {
    var found = false;
    for (var i = 0; i < w.frames.length; i++) {
        try
        {
            if (w.frames[i].location.href.indexOf("/" + id + "/") != -1) {
                w.frames[i].location.reload();
                found = true;
                break;
            }

            found = scanFramesForPlugin(w.frames[i], id);
            if (found)
                break;
        }
        catch (err)
        {
        }
    }
    return found;
}




var MethodWorx = new Object();
MethodWorx.CMS = new Object();
MethodWorx.CMS.Toolbar = new Object();
MethodWorx.CMS.saveProperties = new Array();

MethodWorx.CMS.showPleaseWait = function () {
    var w = window;
    while (w != w.parent && w.parent != null) {
        w = w.parent;
    }

    w.showPleaseWait();
}

MethodWorx.CMS.hidePleaseWait = function () {
    var w = window;
    while (w != w.parent && w.parent != null) {
        w = w.parent;
    }

    w.hidePleaseWait();
}

MethodWorx.CMS.redirect = function (url) {
    var w = window;
    while (w != w.parent && w.parent != null) {
        w = w.parent;
    }

    w.location.href = url;
}

MethodWorx.CMS.showPreview = function (url) {
    var w = window;
    while (w != w.parent && w.parent != null) {
        w = w.parent;
    }

    w.showPreview(url);
}

MethodWorx.CMS.hidePreview = function () {
    var w = window;
    while (w != w.parent && w.parent != null) {
        w = w.parent;
    }

    w.hidePreview();
}

MethodWorx.CMS.getBaseUrl = function (path) {
    var w = window;
    while (w != w.parent && w.parent != null) {
        w = w.parent;
    }
    if (path == null)
        return w.baseUrl;
    else {
        if (path.substring(0, 1) == "/")
            path = path.substr(1);
        return w.baseUrl + path;
    }
}

MethodWorx.CMS.Toolbar.resetToolbar = function (context, hideDefault) {
    if (context == null)
        context = "#customToolbar";
    if (!hideDefault)
        $("#toolbarContainer").show();
    $(context).html("");
};

MethodWorx.CMS.Toolbar.setButtons = function (buttons, context, hideDefault) {
    if (context == null)
        context = "#customToolbar";
    MethodWorx.CMS.Toolbar.resetToolbar(context, hideDefault);
    var html = "";
    for (var i = 0; i < buttons.length; i++) {
        if (buttons[i].options == null) {
            if (buttons[i].type == "label") {
                html += "<span style=\"font-size:8pt;padding-left:8px;\">" + buttons[i].text + "</span>";
            }
            else if (buttons[i].icon != null)
                html += "<button type=\"button\" id=\"" + buttons[i].id + "\" title=\"" +  buttons[i].text + "\" value=\"save\" class=\"toolbar-button\" style=\"background-image:url(" + buttons[i].icon + ");background-repeat:no-repeat;\"></button>";
            else
                html += "<button type=\"button\" id=\"" + buttons[i].id + "\" value=\"save\">" + buttons[i].text + "</button>";
        }
        else {
            html += "<select id=\"" + buttons[i].id + "\" class=\"ui-toolbar-dropdown\" style=\"font-size:10pt\">";
            for (var j = 0; j < buttons[i].options.length; j++) {
                if (buttons[i].options[j].text != null)
                    html += "<option value=\"" + buttons[i].options[j].value + "\">" + buttons[i].options[j].text + "</option>"
                else
                    html += "<option>" + buttons[i].options[j] + "</option>"
            }
            html += "</select>";
        }
    }

    $(context).html(html);

    for (var i = 0; i < buttons.length; i++) {
        if (buttons[i].options == null && buttons[i].type != "label") {
            $('#' + buttons[i].id).button({
                text: buttons[i].hasText
            });
            if (buttons[i].click != null)
                $('#' + buttons[i].id).click(buttons[i].click);
        }
        else {
            if (buttons[i].change != null)
                $('#' + buttons[i].id).change(buttons[i].change);
        }
    }
};

$.fn.disableSelection = function () {
    $(this).attr('unselectable', 'on')
           .css('-moz-user-select', 'none')
           .each(function () {
               this.onselectstart = function () { return false; };
           });
       };

function showUrlPickerAsDialog(filter, onselect) {
    showUrlPicker(filter, true, onselect);
}

MethodWorx.CMS.pickUrl = function (filter, onselect) {
    showUrlPicker(filter, true, onselect);
}

function showUrlPicker(filter, dialog, onselect) {
    $("#parentPath").unbind("click");

    $("#parentPath").click(function () {
        var url = urls.pop();
        url = urls[urls.length - 1];
        loadUrls(filter, url.parentContextType, url.parentContextId, url.parentParentPath, url.parentCurentPath);
    });
    

    if (dialog == true) {
        $("#urlPicker").css("position", "static");
        $("#urlPicker").dialog({
            autoOpen: true,
            title: "Pick",
            width: 303,
            height: 310,
            modal: true,
            resizable: false,
            buttons:
                    {
                        "OK": function () { onselect($("#urls").find(".url.selected")); $("#urlPicker").dialog("close"); },
                        "Cancel": function () { $("#urlPicker").dialog("close"); }
                    }
        });
    }
    else {

        //
        //  ok, show the URK picker
        var p = editor.cursorCoords();

        var top = p.y + 15;
        var left = p.x;
        //
        //  fit the div on the window
        if (top + 200 > $(window).height())
            top = $(window).height() - 210;
        if (left + 300 > $(window).width())
            left = $(window).width() - 310;

        $("#urlPicker").css("left", left);
        $("#urlPicker").css("top", top);
        $("#urlPicker").show();
    }

    getUrls(filter, null, null, "", "$");


}

 var urls = new Array();

        function getUrls(filter, contextType, contextId, parentPath, currentPath) {
            urls.push(
                {
                    parentContextId: contextId,
                    parentContextType: contextType,
                    parentParentPath: parentPath,
                    parentCurentPath: currentPath
                });

            loadUrls(filter, contextType, contextId, parentPath, currentPath);
        }
        var selectedUrl;
        function loadUrls(filter, contextType, contextId, parentPath, currentPath) {

            var obj = new Object();

            $("#currentPath").html(currentPath);
            $("#parentPath").html(parentPath);

            if (contextType != null)
                obj["contextType"] = contextType;
            if (contextId != null)
                obj["contextId"] = contextId;

            if(filter != null)
                obj["filter"] = filter;

            $.ajax({
                type: 'POST',
                url: appRoot + "Content/GetUrlsForPicker",
                data: obj,
                cache: false,
                success: function (data) {
                    if (data.length == 0) {
                        $("#urls").html("");
                        $("#urls").append("<div class=\"url\"><em>No Virtual Urls Found</em></div>");
                    }
                    else {
                        //
                        //  we have the results
                        $("#urls").html("");
                        for (var i = 0; i < data.length; i++) {
                            //  ok, add the url in
                            var t = data[i].Type;
                            if (data[i].IsFolder)
                                t = "folder";
                            $("#urls").append("<div path=\"" + data[i].Path + "\" name=\"" + data[i].Name + "\" isFolder=\"" + data[i].IsFolder + "\" contextType=\"" + data[i].Type + "\" contextId=\"" + data[i].Id + "\" title=\"" + data[i].Path + "\" class=\"url\"><img align=\"absmiddle\" style=\"width:16px;height:16px;\" src=\"" + resolveUrl("~/Images/entities/" + t + ".png") + "\"/>&nbsp;" + data[i].Name + "</div>");
                        }

                        $("#urls").find(".url").dblclick(function () {

                            if ($(this).attr("isFolder").toLowerCase() == "true") {
                                getUrls(filter, $(this).attr("contextType"), $(this).attr("contextId"), parentPath + currentPath, "/" + $(this).attr("name"));
                            }
                            else {
                                editor.replaceSelection($(this).attr("path"));
                                $("#urlPicker").hide();
                            }
                        });

                        $("#urls").find(".url").click(function () {
                            $("#urls").find(".url.selected").removeClass("selected");
                            $(this).addClass("selected");
                        });
                    }
                },
                dataType: "json"
            });
        }

        String.prototype.startsWith = function (str) {
            return (this.indexOf(str) === 0);
        }

        String.prototype.endsWith = function (suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; };

